************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Figure 6 (Night-Sleep and Nap Treatment Effects throughout the Study)
* Last edited: 07 May 2021
************************************************

************************************************	
*0. Initial Setup
************************************************

	clear all
	set more off
	set matsize 800

*******************
** Set-up **
*******************
use "$d/typing_dataset.dta", clear
drop if day_in_study == . | day_in_study == 1

* Generating day bins
	local bin = 3
	gen day_bin_3 = ceil((day_in_study - 9)/`bin') + 2 if day_in_study > 9
	replace day_bin_3 = 3 if day_in_study == 9
	replace day_bin_3 = ceil((day_in_study - 1)/`bin') if day_in_study<8
	replace day_bin_3 = 2 if day_in_study == 8
	replace day_bin_3 = 0 if day_in_study==1
	tab day_in_study day_bin_3
	
	foreach var in prod_work prod_nap prod_ns time_work time_nap time_ns earn_work earn_nap earn_ns {
		gen coef_`var' = 0
	}
	
	foreach var in earn_nap prod_nap time_nap earn_ns prod_ns time_ns {
		gen ub_`var'=0
		gen lb_`var'=0
	}
				
	rename productivity prod
	rename earnings earn
	rename typing_time_hr time

	replace time = time*60

 ** Panel C: Productivity **	***************************************************************************
	gen day_nap= day_bin_3 * treat_nap
	gen day_extra = day_bin_3  * extra_work
	gen day_break = day_bin_3  * break
	gen day_night_sleep = day_bin_3  * treat_pool	
	cap gen day_main = .
		
	preserve
	eststo clear
	
	capture drop _baseline baseline
	gen _baseline			= prod if post_treatment==0
	egen baseline 	= mean(_baseline), by(pid)
	
	
		*Comparison: Nap vs. Break (include Work in regression)
	
		replace day_main = day_nap
		
		eststo nap_break: reghdfe prod i.day_main i.day_night_sleep i.day_extra if post_treatment==1 & at_present_check==1 & day_in_study<28, ///
		cluster(pid) absorb(date female#day_bin_3 long_day#day_bin_3 c.age#day_bin_3 c.baseline#day_bin_3 c.fraction_high#day_bin_3 i.day_type#day_bin_3 day_in_study)
		
		*Comparison: Nap vs. Work (include Break in regression)
		
		replace day_main = day_extra
		
		eststo nap_work: reghdfe prod i.day_main i.day_night_sleep i.day_nap if post_treatment==1 & at_present_check==1 & day_in_study<28, ///
		cluster(pid) absorb(date female#day_bin_3 long_day#day_bin_3 c.age#day_bin_3 c.baseline#day_bin_3 c.fraction_high#day_bin_3 i.day_type#day_bin_3 day_in_study)
		
		*Comparison: Night Sleep vs. Control
		
		replace day_main = day_night_sleep
		
		eststo night_sleep: reghdfe prod i.day_extra i.day_main i.day_nap if post_treatment==1 & at_present_check==1 & day_in_study<28, ///
		cluster(pid) absorb(date female#day_bin_3 long_day#day_bin_3 c.age#day_bin_3 c.baseline#day_bin_3 c.fraction_high#day_bin_3 i.day_type#day_bin_3 day_in_study)
		
		coefplot ///
			(nap_work, offset(.1) keep(*day_main) label(Nap) ciopts(lcol(grey%30)) mcolor(grey%30) msymbol(square) msize(medlarge) ) ///
			(nap_break, keep(*day_main) label(Break) mcolor("62 97 142") msymbol(triangle) msize(medlarge) ciopts(lcol("62 97 128"))) ///
			(night_sleep, offset(-.1) keep(*day_main) label(N-S) mcolor("155 45 15") msymbol(circle) msize(medlarge) ciopts(lcol("155 45 15"))), ///
			xtitle(Days in Study, height(6) size(large)) ytitle("Productivity", height(6) size(large)) ///
			graphregion(fcolor(white)) vertical level(90) ///
			yline(0, lcolor("25 25 25"))   ylab(-150(100)300, labsize(medlarge)) ysc(r(-150 300)) ///
			xlabel( 1 "9-12" 2 "13-15" 3 "16-18" 4 "19-21" 5 "22-24" 6 "25-27" , labsize(medlarge) ) ///
			legend(order(6 "Night-Sleep" 4 "Nap" 2 "Work") region(lcolor(white)) position(2) ring(0) bplacement(neast) cols(3) symysize(2) symxsize(2.5) size(6)) graphregion(fcolor(white)) ///
			xsize(1.8) ysize(1)	
	
	graph export "$oa/Figures/Figure_A6/prod_by_day.eps", replace
	graph export "$oa/Figures/Figure_A6/prod_by_day.pdf", replace
	graph export "$oa/Figures/Figure_A6/prod_by_day.png", replace
	
	**** Panel B: Labor Supply (Typing Time) ********************************************
	
	eststo clear
	
	capture drop _baseline baseline
	gen _baseline			= time if post_treatment==0
	egen baseline 	= mean(_baseline), by(pid)
	
	
		*Comparison: Nap vs. Break (include Work in regression)
	
		replace day_main = day_nap
		
		eststo nap_break: reghdfe time i.day_main i.day_night_sleep i.day_extra if post_treatment==1 & at_present_check==1 & day_in_study<28, ///
		cluster(pid) absorb(date female#day_bin_3 long_day#day_bin_3 c.age#day_bin_3 c.baseline#day_bin_3 c.fraction_high#day_bin_3 i.day_type#day_bin_3 day_in_study)
		
		*Comparison: Nap vs. Work (include Break in regression)
		
		replace day_main = day_extra
		
		eststo nap_work: reghdfe time i.day_main i.day_night_sleep i.day_nap if post_treatment==1 & at_present_check==1 & day_in_study<28, ///
		cluster(pid) absorb(date female#day_bin_3 long_day#day_bin_3 c.age#day_bin_3 c.baseline#day_bin_3 c.fraction_high#day_bin_3 i.day_type#day_bin_3 day_in_study)
		
		*Comparison: Night Sleep vs. Control
		
		replace day_main = day_night_sleep
		
		eststo night_sleep: reghdfe time i.day_extra i.day_main i.day_nap if post_treatment==1 & at_present_check==1 & day_in_study<28, ///
		cluster(pid) absorb(date female#day_bin_3 long_day#day_bin_3 c.age#day_bin_3 c.baseline#day_bin_3 c.fraction_high#day_bin_3 i.day_type#day_bin_3 day_in_study)
		
	coefplot ///
			(nap_work, offset(.1) keep(*day_main) label(Nap) mcolor(grey%30) msymbol(square) msize(medlarge) ciopts(lcol(grey%30))) ///
			(nap_break, keep(*day_main) label(Break) mcolor("62 97 142") msymbol(triangle) msize(medlarge) ciopts(lcol("62 97 128"))) ///
			(night_sleep, offset(-.1) keep(*day_main) label(N-S) mcolor("155 45 15") msymbol(circle) msize(medlarge) ciopts(lcol("155 45 15"))), ///
			xtitle(Days in Study, height(6) size(large)) ytitle("Time Typing (Minutes)", height(6) size(large)) ///
			graphregion(fcolor(white)) level(90)  ///
			yline(0, lcolor("25 25 25"))  vertical ylab(-15(15)45, labsize(medlarge)) ysc(r(-15 55)) ///
			xlabel( 1 "9-12" 2 "13-15" 3 "16-18" 4 "19-21" 5 "22-24" 6 "25-27" , labsize(medlarge) ) ///
			legend(order(6 "Night-Sleep" 4 "Nap" 2 "Work") region(lcolor(white)) position(2) ring(0) bplacement(neast) cols(3) symysize(2) symxsize(2.5) size(6)) graphregion(fcolor(white)) ///
			xsize(1.8) ysize(1)
	
	graph export "$oa/Figures/Figure_A6/typing_time_by_day.png", replace 
	graph export "$oa/Figures/Figure_A6/typing_time_by_day.pdf", replace
	graph export "$oa/Figures/Figure_A6/typing_time_by_day.eps", replace

	
		**** Panel A:  Earnings **************************************************************
	
	capture drop _baseline baseline
	gen _baseline			= earn if post_treatment==0
	egen baseline 	= mean(_baseline), by(pid)
	
	
		*Comparison: Nap vs. Break (include Work in regression)
	
		replace day_main = day_nap
		
		eststo nap_break: reghdfe earn i.day_main i.day_night_sleep i.day_extra if post_treatment==1 & at_present_check==1 & day_in_study<28, ///
		cluster(pid) absorb(date female#day_bin_3 long_day#day_bin_3 c.age#day_bin_3 c.baseline#day_bin_3 c.fraction_high#day_bin_3 i.day_type#day_bin_3 day_in_study)
		
		*Comparison: Nap vs. Work (include Break in regression)
		
		replace day_main = day_extra
		
		eststo nap_work: reghdfe earn i.day_main i.day_night_sleep i.day_nap if post_treatment==1 & at_present_check==1 & day_in_study<28, ///
		cluster(pid) absorb(date female#day_bin_3 long_day#day_bin_3 c.age#day_bin_3 c.baseline#day_bin_3 c.fraction_high#day_bin_3 i.day_type#day_bin_3 day_in_study)

		*Comparison: Night Sleep vs. Control
		
		replace day_main = day_night_sleep
		
		eststo night_sleep: reghdfe earn i.day_extra i.day_main i.day_nap if post_treatment==1 & at_present_check==1 & day_in_study<28, ///
		cluster(pid) absorb(date female#day_bin_3 long_day#day_bin_3 c.age#day_bin_3 c.baseline#day_bin_3 c.fraction_high#day_bin_3 i.day_type#day_bin_3 day_in_study)

	coefplot ///
			(nap_work, offset(.1) keep(*day_main) label(Nap) mcolor(grey%30) msymbol(square) msize(medlarge) ciopts(lcol(grey%30))) ///
			(nap_break, keep(*day_main) label(Break) mcolor("62 97 142") msymbol(triangle) msize(medlarge) ciopts(lcol("62 97 128"))) ///
			(night_sleep, offset(-.1) keep(*day_main) label(N-S) mcolor("155 45 15") msymbol(circle) msize(medlarge) ciopts(lcol("155 45 15"))), ///
			xtitle(Days in Study, height(6) size(large)) ytitle("Earnings", height(6) size(large)) ///
			graphregion(fcolor(white)) level(90)  ///
			yline(0, lcolor("25 25 25"))  vertical ylab(-15(15)60, labsize(medlarge)) ysc(r(-15 75)) ///
			xlabel( 1 "9-12" 2 "13-15" 3 "16-18" 4 "19-21" 5 "22-24" 6 "25-27", labsize(medlarge) ) ///
			legend(order(6 "Night-Sleep" 4 "Nap" 2 "Work") region(lcolor(white)) position(2) ring(0) bplacement(neast) cols(3) symysize(2) symxsize(2.5) size(6)) graphregion(fcolor(white)) ///
			xsize(1.8) ysize(1)
	
	graph export "$oa/Figures/Figure_A6/earn_by_day.eps", replace 
	graph export "$oa/Figures/Figure_A6/earn_by_day.pdf", replace
	graph export "$oa/Figures/Figure_A6/earn_by_day.png", replace
	
restore
